package com.sinoyd.itszyw.Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @createBy : Sinoyd 大饼洲
 * @description :
 * @projectName : ITYW
 * @creationDate: 2017-04-11 15:33
 */
public class ImageUrlUtils {


    /**
     * 获取图片路径里的文件名
     *
     * @param imageList
     * @return
     */
    public static String getPicNameFromPath(List<String> imageList) {
        String imageName = "";
        for (int i = 0; i < imageList.size() - 1; i++) {
            String picturePath = imageList.get(i);
            String temp[] = picturePath.replaceAll("\\\\", "/").split("/");
            String fileName = "";
            if (temp.length > 1) {
                fileName = temp[temp.length - 1];
            }
            if (i == 0) {
                imageName = fileName;
            } else {
                imageName = imageName + "," + fileName;

            }
        }


        return imageName;
    }

    /**
     * 吧图片转成 base64字节码传输
     *
     * @param imageList
     * @return
     */
    public static String getImageStr(List<String> imageList) {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        String imageNameUrl = "";
        for (int i = 0; i < imageList.size() - 1; i++) {
            InputStream in = null;
            byte[] data = null;
            String fileNameUrl = "";
            //读取图片字节数组
            try {
                in = new FileInputStream(new File(imageList.get(i)));
                data = new byte[in.available()];
                in.read(data);
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            fileNameUrl = new String(android.util.Base64.encode(data, android.util.Base64.DEFAULT));
            if (i == 0) {
                imageNameUrl = fileNameUrl;
            } else {
                imageNameUrl = imageNameUrl + "," + fileNameUrl;

            }
        }
        return imageNameUrl;

    }

}
